﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using System.IO;

namespace Surfium
{
    public enum FormType
    {
        InputText,
        InputPassword,
        Submit,
        Button

    }
    public partial class MyMenuItem : ElementMenuItem
    {

        private String label;
        public String Label
        {
            get { return label; }
            set
            {
                label = value;
                inner_label.Text = value;
            }
        }

        private MyWindow window;
        public MyWindow Window
        {
            get { return window; }
            set { window = value; }
        }

        private String link;
        public String Link
        {
            get { return link; }
            set { link = value; }
        }

        ElementMenu parent;
        public ElementMenu Parent_menu
        {
            get { return parent; }
            set { parent = value; }
        }


        private int id;
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        private FormType type;

        private String alt;

        private Boolean dc = false;

        private Boolean draggable = false;

        public delegate void ChangingHandler(object sender, LinkArgs aa);
        public delegate void ChangingOverHandler(object sender, OverArgs aa);

        public MyMenuItem(String link, String label, String alt, String src, int id, int dc, MyWindow window, ElementMenu parent)
        {
            this.InitializeComponent();



            ImageSource = src;
            if (Properties.Settings.Default.EmptyStringRepre.Equals(label))
            {
                if (!Properties.Settings.Default.EmptyStringRepre.Equals(alt))
                {
                    Label = alt;
                }
                else
                {
                    Label = "Image";
                }
            }
            else { Label = label; }

            this.alt = alt;
            Link = link;

            Id = id;
            Window = window;
            Parent_menu = parent;

            directContact(dc == 1);
            checkDraggable();
            AddHandlers();

        }
        public MyMenuItem(String link, String label, int id, int dc, MyWindow window, ElementMenu parent)
        {
            InitializeComponent();


            Label = label;
            Link = link;
            Id = id;
            Window = window;
            Parent_menu = parent;

            directContact(dc == 1);
            checkDraggable();
            AddHandlers();
        }
        public MyMenuItem(int id, int type, String label, int dc, MyWindow window, ElementMenu parent)
        {
            InitializeComponent();
            Link = null;
            Id = id;
            Window = window;
            Parent_menu = parent;
            alt = label;
            switch (type)
            {
                case 0:
                    this.type = FormType.InputText;
                    Label = "Text Input" + (String.Empty.Equals(alt) ? String.Empty : " (" + alt + ")");
                    break;
                case 1:
                    this.type = FormType.InputPassword;
                    Label = "Password Input";
                    break;
                case 2:
                    this.type = FormType.Submit;
                    Label = (String.Empty.Equals(alt) ? "Submit button" : alt + " (Submit button)");
                    break;
                case 3:
                    this.type = FormType.Button;
                    Label = (String.Empty.Equals(alt) ? "Button" : alt + " (Submit button)");
                    break;
                default: throw new Exception("Unexpected type " + type);

            }

            directContact(dc == 1);
            checkDraggable();
            AddHandlers();

        }
        private void AddHandlers()
        {

            PreviewContactDown += new ContactEventHandler(DragSourcePreviewInputDeviceDown);
            Parent_menu.GotContactCapture += new ContactEventHandler(parent_GotContactCapture);
            PreviewContactUp += new ContactEventHandler(MyMenuItem_ContactUp);
        }


        void MyMenuItem_ContactUp(object sender, ContactEventArgs e)
        {
            if (link != null)
            {
                if (link.Contains("javascript:void(0)"))
                {
                    Window.emulateClick(id);

                }
                else
                {
                    Window.navigate(link);
                }
                //
            }
            else
            {
                switch (type)
                {

                    case FormType.InputPassword:
                    case FormType.InputText:
                        Window.injectInput(id, type, alt);
                        break;
                    case FormType.Button:
                    case FormType.Submit:
                        Window.emulateClick(id);
                        break;
                }

            }
        }

        void parent_GotContactCapture(object sender, ContactEventArgs e)
        {

            if (IsPressed)
            {
                e.Handled = true;
                Window.BlinkLink(Id);
            }
        }


        String ImageSource
        {
            set
            {
                BitmapImage bi;
                if (!Properties.Settings.Default.ForceActualImages)
                {

                    bi = new BitmapImage();
                    MemoryStream ms = new MemoryStream();
                    Properties.Resources.img_ico.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    bi.BeginInit();

                    bi.StreamSource = new MemoryStream(ms.ToArray());
                    bi.StreamSource = ms;
                    bi.EndInit();
                    inner_image.Source = bi;
                }
                else
                {

                    try
                    {
                        bi = new BitmapImage();
                        bi.BeginInit();
                        bi.UriSource = new Uri(value);
                        bi.EndInit();
                        inner_image.Source = bi;
                    }
                    catch (UriFormatException)
                    {
                        bi = new BitmapImage();
                        MemoryStream ms = new MemoryStream();
                        Properties.Resources.img_ico.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        bi.BeginInit();

                        bi.StreamSource = new MemoryStream(ms.ToArray());
                        bi.StreamSource = ms;
                        bi.EndInit();
                        inner_image.Source = bi;


                    }
                }
            }
        }


        private void DragSourcePreviewInputDeviceDown(object sender, ContactEventArgs e)
        {
            FrameworkElement findSource = e.OriginalSource as FrameworkElement;
            MyMenuItem draggedElement = null;

            // Find the ScatterViewItem object that is being touched.
            while (draggedElement == null && findSource != null)
            {
                if ((draggedElement = findSource as MyMenuItem) == null)
                {
                    findSource = VisualTreeHelper.GetParent(findSource) as FrameworkElement;
                }
            }

            if (draggedElement == null)
            {
                return;
            }


            if (!draggable)
            {
                return;
            }

            /*
            try
            {
                try
                {
                    Uri u = new Uri(link);
                }
                catch (UriFormatException)
                {
                    Uri uri = new Uri(window.Source);
                    link = new Uri(uri, link).AbsoluteUri;
                }
            }
            catch (Exception)
            {
                return;
            }*/


            MyDragCursor cursorVisual = new MyDragCursor(label, link, dc, inner_image.Source, window);




            List<InputDevice> devices = new List<InputDevice>();
            devices.Add(e.Device);

            foreach (InputDevice device in draggedElement.ContactsCapturedWithin)
            {
                if (device != e.Device)
                {
                    devices.Add(device);
                }
            }

            ItemsControl dragSource = ItemsControl.ItemsControlFromItemContainer(draggedElement);

            Boolean created =
                SurfaceDragDrop.BeginDragDrop(
                  dragSource,
                  draggedElement,
                  cursorVisual,
                  draggedElement.DataContext,
                  devices,
                  DragDropEffects.Move);

            if (created)
            {
                draggedElement.Visibility = System.Windows.Visibility.Hidden;
                draggedElement.window.hideMenu_Animate();
                e.Handled = true;
            }
        }
        private void directContact(Boolean dc)
        {
            

            this.dc = dc;

            if (dc)
            {
                //Background = (RadialGradientBrush)Resources["OsmoBrush"];
                Height = Properties.Settings.Default.MenuItemDCSize;
                Width = Properties.Settings.Default.MenuItemDCSize;
            }
            else
            {
                Height = Properties.Settings.Default.MenuItemSize;
                Width = Properties.Settings.Default.MenuItemSize;
            }

        }
        private void checkDraggable()
        {


            try
            {
                try
                {
                    Uri u = new Uri(link);
                }
                catch (UriFormatException)
                {
                    Uri uri = new Uri(window.Source);
                    link = new Uri(uri, link).AbsoluteUri;
                }
            }
            catch (Exception)
            {
                Background = (RadialGradientBrush)Resources["OsmoBrush"];
                return;
            }
            
            draggable = true;
        }


    }
}